<template>
  <section class="search">
    <header-top title="搜索"></header-top>
    <form class="search_form" action="#">
      <input
        type="search"
        name="search"
        placeholder="请输入商家或美食名称"
        class="search_input"
        v-model="keywords"
      />
      <input type="submit" name="submit" class="search_submit" @click.prevent="search" />
    </form>
    <section class="list" v-if="searchShop.length">
      <ul class="list_container">
        <router-link
          to="/shop"
          tag="li"
          class="list_li"
          v-for="(item, index) in searchShop"
          :key="item._id"
        >
          <section class="item_left">
            <img :src="item.pic" class="restaurant_img" />
          </section>
          <section class="item_right">
            <div class="item_right_text">
              <p>
                <span>{{ item.name }}</span>
              </p>
              <p>月售 {{ item.count }} 单</p>
              <p>{{item.price}} 元起送 / 距离{{item.distance}}m</p>
            </div>
          </section>
        </router-link>
      </ul>
    </section>

    <div class="search_none" v-if="noSearchResult">很抱歉！无搜索结果</div>
  </section>
</template>
 
<script>
import HeaderTop from "../../components/HeaderTop/HeaderTop";
import { mapState } from "vuex";
export default {
  data() {
    return {
      keywords: "",
      noSearchResult: false
    };
  },
  methods: {
    search() {
      this.$store.dispatch("getSearchShop", this.keywords);
    }
  },
  components: {
    HeaderTop
  },
  computed: {
    ...mapState(["searchShop"])
  },
  watch: {
    searchShop(value) {
      if (value.length > 0) {
        console.log(false);
        this.noSearchResult = false;
      } else {
        console.log(true);
        this.noSearchResult = true;
      }
    }
  }
};
</script>
 
<style lang="stylus" rel="stylesheet/stylus" scoped>
@import '../../common/stylus/minxins.styl';

.search {
  width: 100%;
  height: 100%;
  overflow: hidden;

  .search_form {
    clearFix();
    margin-top: 45px;
    background-color: #fff;
    padding: 12px 8px;

    input {
      height: 35px;
      padding: 0 4px;
      border-radius: 2px;
      font-weight: bold;
      outline: none;

      &.search_input {
        float: left;
        width: 79%;
        border: 4px solid #f2f2f2;
        font-size: 14px;
        color: #333;
        background-color: #f2f2f2;
      }

      &.search_submit {
        float: right;
        width: 18%;
        border: 4px solid #02a774;
        font-size: 16px;
        color: #fff;
        background-color: #02a774;
      }
    }
  }

  .list {
    .list_container {
      background-color: #fff;

      .list_li {
        display: flex;
        justify-content: center;
        padding: 10px;
        border-bottom: 1px solid $bc;

        .item_left {
          margin-right: 10px;

          .restaurant_img {
            width: 50px;
            height: 50px;
            display: block;
          }
        }

        .item_right {
          font-size: 12px;
          flex: 1;

          .item_right_text {
            p {
              line-height: 12px;
              margin-bottom: 6px;

              &:last-child {
                margin-bottom: 0;
              }
            }
          }
        }
      }
    }
  }

  .search_none {
    margin: 0 auto;
    color: #333;
    background-color: #fff;
    text-align: center;
    margin-top: 0.125rem;
  }
}
</style>
